package com.hw.springcloud.service.impl;

import cn.hutool.core.lang.UUID;
import com.hw.springcloud.service.IMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

import javax.annotation.Resource;

/**
 * @Classname: IMessageImpl
 * @Description: 接口实现类
 * @Date: 2021/12/3
 * @Author: HuWei
 */
@EnableBinding(Source.class) // 定义消息推送管道,将Channel和Exchange绑定在一起
public class IMessageProviderImpl implements IMessageProvider {

    // 定义消息管道,要用@Rersource注解注入
    @Resource
    private MessageChannel output;

    @Override
    public String sendMessage() {
        String msg = UUID.fastUUID().toString();
        // 构建消息
        Message<String> msgBuild = MessageBuilder.withPayload(msg).build();
        // 发送消息
        output.send(msgBuild);
        return msg;
    }
}
